#!/usr/bin/env python

# vPresent is an immersive presentation creation and display application.
# Copyright (C) 2007 by Infiscape Corporation
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

import sys

import osg2 as osg
from OpenGL.GL import *
from OpenGL.GLUT import *


server = None
ract   = None
window = None

def usage():
   print "%s -m -p -w -a address" % sys.argv[0]

def display():
   try:
      server.render(ract)
   except Exception, ex:
      #print ex
      server.stop()
      glutHideWindow()
      server.start()
      glutShowWindow()

   # It seems like this should happen regardless of whether an exception is
   # thrown.
   osg.Thread.getCurrentChangeList().clear()

def update():
   glutPostRedisplay()

def reshape(width, height):
   window.resize(width, height)

def main():
   global server, ract

   name            = 'ClusterServer'
   connection_type = 'StreamSock'
   fullscreen      = True
   address         = ''

   glutInit(sys.argv)
   glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE)

   args = sys.argv[1:]
   while len(args) > 0:
      arg = args.pop(0)

      if arg.startswith('-'):
         if arg[1] == 'm':
            connection_type = 'Multicast'
         elif arg[1] == 'p':
            connection_type = 'SockPipeline'
         elif arg[1] == 'w':
            fullscreen = False
         elif arg[1] == 'a':
            try:
               if len(arg) > 2:
                  address = arg[2:]
               else:
                  address = args.pop(0)
            except:
               pass
      else:
         name = arg

   try:
      osg.osgInit(sys.argv)

      winid = glutCreateWindow(name)
      glutDisplayFunc(display)
      glutIdleFunc(update)
      glutReshapeFunc(reshape)

      glEnable(GL_LIGHTING)
      glEnable(GL_LIGHT0)
      glEnable(GL_NORMALIZE)

      ract = osg.RenderAction.create()

      global window
      window = osg.GLUTWindow.create()
      window.setId(winid)
      window.init()

      server = osg.ClusterServer(window, name, connection_type, address)
      server.start()

      # This goes after the cluster server is started so that the GLUT window
      # is not opened before we have a connection.
      if fullscreen:
         glutFullScreen()

      # The same for this.
      glutSetCursor(GLUT_CURSOR_NONE)

      glutMainLoop()
   except Exception, ex:
      print ex
      osg.osgExit()

   sys.exit(0)

if __name__ == "__main__":
   main()
